import config from '../consts/config.js';
import store from '../store/index.js';

const httpHelper = {
	/**
	 * TODO 登录失败处理函数
	 * @method loginFailHandler
	 * */
	loginFailHandler() {

	},
	/**
	 * 反馈请求结果
	 * @method 
	 * */
	showToastRes(icon, title) {
		uni.showToast({
			icon,
			title,
			duration: 1000
		});
	}
};


/**
 * http 工具类
 * @class 
 * @constructor 
 * */
class http {

	constructor(BASEURL) {
		this.BASEURL = BASEURL;
		this.SUCCESSCODE = 200;
	}

	/**
	 * request
	 * @method request
	 * @param {String}   
	 * */
	request(url, method, data, header = null, isLoading = false, loadingTxt = '') {
		const that = this;
		if (isLoading) {
			uni.showLoading({
				title: loadingTxt,
				mask: true
			});
		}
		let headers = {
			'Accept': 'application/json',
			'content-type': 'application/json',
			'token': store.state.user.userToken || undefined
		};
		if (header) {
			Object.assign(headers, header);
		}
		let resMsg = {
			header: headers,
			url,
			method,
			data,
			result: null,
			isLoading,
			loadingTxt
		};
		return new Promise((resolve, reject) => {
			uni.request({
				header: headers,
				url: that.BASEURL + url,
				method,
				data,
				success(res) {
					resMsg.result = res;
					console.log(`requestSuccess---->${res.data.code}`, resMsg);
					getApp().globalData.Log.info(JSON.stringify(resMsg));
					getApp().globalData.Log.setFilterMsg('requestMsg');
					console.log(getApp().globalData.Log);
					if (isLoading) uni.hideLoading();
					that.successHandler(res, resolve, reject);
				},
				fail(err) {
					resMsg.result = err;
					console.log(`requestFail---->`, resMsg);
					getApp().globalData.Log.error(JSON.stringify(resMsg));
					getApp().globalData.Log.setFilterMsg('requestMsg');
					console.log(getApp().globalData.Log);
					if (isLoading) uni.hideLoading();
					that.failHandler(err, reject);
				}
			});
		});
	}

	/**
	 * 请求成功处理函数
	 * @method successHandler
	 * */
	successHandler(res, resolve, reject) {
		let rescode = res.data.code;
		if (res.statusCode === this.SUCCESSCODE && rescode === this.SUCCESSCODE) {
			resolve(res.data);
		} else if (rescode === 400 && /^暂无.{1,}数据$/.test(res.data.message)) {
			resolve({
				code: 200,
				data: [],
				message: 'SUCCESS'
			});
		} else {
			let errorText = '';
			switch (rescode) {
				//400
				case 400:
					errorText = res.data.message;
					break;
					// 401: 未登录
				case 401:
					errorText = "未登录，请前往登录";
					break;
					// 403 当前状态不可编辑/修改
				case 403:
					errorText = res.data.message;
					break;
					// 405 没有权限
				case 405:
					errorText = "您暂无权限访问，请联系管理员";
					break;
					// 404请求不存在
				case 404:
					errorText = "接口或资源不存在";
					//TODO:提示用户
					break;
					// 422参数错误
				case 422:
					errorText = "参数错误";
					//TODO:提示用户
					break;
					// 500服务器错误
				case 500:
					errorText = "服务器错误";
					//TODO:提示用户
					break;
					// 其他错误，直接抛出错误提示
				default:
					errorText = "操作异常，请联系技术人员";
					reject(res.data);
			}
			uni.showToast({
				icon: 'none',
				title: errorText
			});
		}
	}

	/**
	 * 请求失败处理函数
	 * @method failHandler
	 * */
	failHandler(err, reject) {
		reject(err);
	}

	get(obj) {
		return this.request(obj.url, 'GET', obj.data, obj.header, obj.isLoading, obj.loadingTxt);
	}

	post(obj) {
		return this.request(obj.url, 'POST', obj.data, obj.header, obj.isLoading, obj.loadingTxt);
	}

	put(obj) {
		return this.request(obj.url, 'PUT', obj.data, obj.header, obj.isLoading, obj.loadingTxt);
	}

	del(obj) {
		return this.request(obj.url, 'DELETE', obj.data, obj.header, obj.isLoading, obj.loadingTxt);
	}
}

export default new http(config.BASEURL);
